package com.mtnz.njk.Service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.mtnz.njk.Mapper.StartImgManageMapper;
import com.mtnz.njk.Util.DeleteFileUtil;
import com.mtnz.njk.entity.StartImgEntity;
import com.mtnz.njk.result.PageInfo;
import com.mtnz.njk.result.ResponseMessage;
import com.mtnz.njk.result.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.*;

/**
 * @Description: 载入页
 * @Author: ZPX
 * @CreateDate: 2019/8/26 15:58
 * @Version: 1.0
 */
@Service
public class StartImgManageService {

    @Autowired
    StartImgManageMapper startImgManageMapper;

    /**
     * 载入页列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    public ResponseResult findAllData(Integer pageNum, Integer pageSize) {
        //分页
        PageHelper.startPage(pageNum, pageSize);
        List<StartImgEntity> startImgEntities = startImgManageMapper.selectAll();
        PageInfo pageInfo = new PageInfo<>(startImgEntities);
        return new ResponseResult(ResponseMessage.OK, pageInfo);
    }

    /**
     * 保存图片
     *
     * @param file
     * @param request
     * @return
     */
    public ResponseResult saveImg(MultipartFile file, HttpServletRequest request) throws Exception {
        if (file != null && !"".equals(file)) {
            String fileName = file.getOriginalFilename();// 获取到上传文件的名字
            StartImgEntity startImgEntity = new StartImgEntity();
            startImgEntity.setStartImgName(fileName);
            if (fileName != null && !"".equals(fileName)) {
                List<StartImgEntity> startImgEntities = startImgManageMapper.selectLaunchImg(fileName,null,null);
                if (startImgEntities.size() > 0) {
                    return new ResponseResult(ResponseMessage.FAIL, "名字重复,请重新命名!!!");
                } else {
                    startImgEntity.setStartImgId(IdUtil.simpleUUID());
                    startImgEntity.setCreateTime(DateUtil.now());
                    File dest = new File(new File(DeleteFileUtil.imgpath).getAbsolutePath() + "/" + fileName);
                    if (!dest.getParentFile().exists()) {
                        dest.getParentFile().mkdirs();
                    }
                    file.transferTo(dest); // 保存文件
                    startImgEntity.setStartImgUrl(DeleteFileUtil.path + "/" + fileName);
                    startImgManageMapper.insert(startImgEntity);
                }
            }
        }
        return new ResponseResult(ResponseMessage.OK);
    }

    /**
     * 删除图片
     *
     * @return
     * @throws Exception
     */
    public ResponseResult delImg(String imgId, HttpServletRequest request, HttpServletResponse response) throws Exception {

            List<StartImgEntity> startImgEntities = startImgManageMapper.selectLaunchImg(null, imgId,null);
            String path = DeleteFileUtil.imgpath + "/";
            if (startImgEntities.size() > 0) {
                path += startImgEntities.get(0).getStartImgName();
            }
            //删除电脑存储图片
            DeleteFileUtil.deleteFile(path);
            //删除数据库记录
            startImgManageMapper.deleteByPrimaryKey(imgId);

       return new ResponseResult(ResponseMessage.OK);
    }

    /**
     * 图片名字和当天日期对比，自动获取图片
     * @return
     * @throws Exception
     */
    public ResponseResult selectByDate(HttpServletRequest request, HttpServletResponse response)throws Exception{
        //返回模型
        Map map = new HashMap();
        //获取当前时间
        String days = com.mtnz.njk.Util.DateUtil.getDays();
        map.put("day",days);
        //模糊查询
        List<StartImgEntity> startImgEntities = startImgManageMapper.selectLaunchImg(null, null, days);
        if(startImgEntities.size()>0){
            //筛选图片
            map.put("img_url", startImgManageMapper.selectLaunchImg(null,null,days).get(0).getStartImgUrl());
        }else {
            //默认图片
            map.put("img_url", DeleteFileUtil.localpath);
        }
        return new ResponseResult(ResponseMessage.OK,map);
    }
}
